package jsu.lipengcheng.trafficnet.controlle.user;

import jsu.lipengcheng.trafficnet.common.BeanThread;
import jsu.lipengcheng.trafficnet.common.Result;
import jsu.lipengcheng.trafficnet.pojo.Cars;
import jsu.lipengcheng.trafficnet.pojo.News;
import jsu.lipengcheng.trafficnet.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user/car")
public class CarControlle {
    @Autowired
    private CarService carService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询用户的所有车辆
     * @return
     */
    @GetMapping
    public Result getusercar(){
        Integer id = BeanThread.get();
        String key ="carlist"+id;
        List<Cars> carList=(List<Cars>) redisTemplate.opsForValue().get(key);
        if(carList!=null && carList.size()>0 ){
            return Result.success(carList);
        }
        else {
            carList=carService.selectall();
            redisTemplate.opsForValue().set(key,carList,10, TimeUnit.MINUTES);
            return Result.success(carList);
        }
//        List<Cars> carsList= carService.selectcar();
//        return Result.success(carsList);
    }
}
